const { SECRET_KEY } = require('../../config')

module.exports = (req, res, next) => {
  const whiteList = ['/', '/favicon.ico', '/users/login'] // 白名单
  if (whiteList.indexOf(req.url) > -1) {
    return next();
  }

  const Authorization = req.headers.Authorization

  if (!Authorization) {
    return res.status(401).send('缺少用户身份信息')
  }

  const token = Authorization.split(" ")[1];

  jwt.verify(token, SECRET_KEY, (err, decoded) => {
    if (err) {
      console.log("verify error", err);
      return res.json({ code: "401", msg: "token 无效" });
    }
    console.log("verify decoded", decoded);
    next();
  });
};